package com.ohos.carbon.provider;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DataBean;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.app.Context;

import java.util.List;

public class ComponentIconProvider extends RecyclerAdapter<ComponentIconProvider.ViewHolder> {

    private List<DataBean> mList;
    private Context context;

    public ComponentIconProvider(List<DataBean> mList, Context context) {
        this.mList = mList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ComponentContainer componentContainer) {

        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_compoents_icon_provider, componentContainer, false);
        return new ViewHolder(component);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        ViewHolder viewHolder = holder;
        if (mList != null) {
            Element icon = mList.get(position).getIcon();
            Element caveat = mList.get(position).getCaveat();
            Element favorites = mList.get(position).getFavorites();
            viewHolder.icon.setImageElement(icon);
            viewHolder.caveat.setImageElement(caveat);
            viewHolder.favorites.setImageElement(favorites);
            viewHolder.content.setText(mList.get(position).getText());
            viewHolder.content.setMultipleLine(true);
            viewHolder.content.setPaddingRight(110);

        }

    }


    @Override
    public Object getItem(int position) {
        if (mList != null && position >= 0 && position < mList.size()) {
            return mList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class ViewHolder extends RecyclerAdapter.ViewHolder {

        private Image icon, caveat, favorites;
        private Text content;

        public ViewHolder(Component itemComponent) {
            super(itemComponent);
            icon = (Image) itemComponent.findComponentById(ResourceTable.Id_image);
            caveat = (Image) itemComponent.findComponentById(ResourceTable.Id_caveat);
            favorites = (Image) itemComponent.findComponentById(ResourceTable.Id_favorites);
            content = (Text) itemComponent.findComponentById(ResourceTable.Id_title_name);
        }
    }
}
